using System;
using UnityEngine;
using UnityEngine.UIElements;
using MinesweeperPlus.Scripts.Abstractions.Enums;
using MinesweeperPlus.Scripts.Configs;
using MinesweeperPlus.Scripts.Utils;

namespace MinesweeperPlus.Scripts.UI
{
    public sealed class TopPanel : UIElement
    {
        public new class UxmlFactory : UxmlFactory<TopPanel> { }

        #region Constants
        private const int SECONDS_IN_MINUTE = 60;
        private const int MINUTES_IN_HOUR = 60;
        #endregion

        #region Actions
        public Action OnBackButtonClick;
        public Action<PlayfieldToolType> OnToolButtonClick;
        public Action OnMenuButtonClick;
        #endregion

        #region Variables
        private PlayfieldToolType _toolType;

        private VisualElement _timerPanel;
        private Label _timerLabel;

        private Button _backButton;
        private IMGUIContainer _backIcon;

        private Button _toolButton;
        private IMGUIContainer _toolIcon;

        private Button _menuButton;
        private IMGUIContainer _menuIcon;
        #endregion

        #region TopPanel Methods
        public TopPanel() : base() { }
        public TopPanel(IconsConfig iconsConfig, StyleSheet styleSheet) : 
            base(iconsConfig, styleSheet) { }

        public void SetToolButtonType(PlayfieldToolType toolType)
        {
            if (_toolType == toolType)
                return;
                
            _toolType = toolType;
            switch (toolType)
            {
                case PlayfieldToolType.Shovel:
                    _toolIcon.SetBackgroundImage(IconsConfig.ShovelIcon);
                    break;
                case PlayfieldToolType.Flag:
                    _toolIcon.SetBackgroundImage(IconsConfig.FlagIcon);
                    break;
                case PlayfieldToolType.Hand:
                    _toolIcon.SetBackgroundImage(IconsConfig.HandIcon);
                    break;
            }
        }

        public void SetTimerTextFromSeconds(int seconds)
        {
            var s = seconds % SECONDS_IN_MINUTE;
            var m = seconds / MINUTES_IN_HOUR;
            _timerLabel.text = $"{(m < 10 ? "0" : "")}{m}:{(s < 10 ? "0" : "")}{s}";
        }
        
        public void ShowBackButton()
        {
            _backButton.SetDisplay(true);
            _toolButton.SetDisplay(false);
        }
        public void ShowToolButton()
        {
            _toolButton.SetDisplay(true);
            _backButton.SetDisplay(false);
        }

        public void ShowMenuButton()
        {
            _menuButton.SetDisplay(true);
        }
        public void HideMenuButton()
        {
            _menuButton.SetDisplay(false);
        }

        public void HideAllButtons()
        {
            _backButton.SetDisplay(false);
            _toolButton.SetDisplay(false);
            HideMenuButton();
        }
        #endregion

        #region Other Methods
        protected override void UpdateColorScheme()
        {
            base.UpdateColorScheme();

            Panel.SetBackgroundColor(ColorSchemeConfig.BackgroundColor);
            Panel.SetBorderColor(ColorSchemeConfig.BorderColor);

            _backButton.SetBackgroundColor(ColorSchemeConfig.BackgroundColor);
            _backButton.SetBorderColor(ColorSchemeConfig.BorderColor);
            _backIcon.SetBackgroundImageColor(ColorSchemeConfig.IconColor);

            _toolButton.SetBackgroundColor(ColorSchemeConfig.BackgroundColor);
            _toolButton.SetBorderColor(ColorSchemeConfig.BorderColor);
            _toolIcon.SetBackgroundImageColor(ColorSchemeConfig.IconColor);

            _menuButton.SetBackgroundColor(ColorSchemeConfig.BackgroundColor);
            _menuButton.SetBorderColor(ColorSchemeConfig.BorderColor);
            _menuIcon.SetBackgroundImageColor(ColorSchemeConfig.IconColor);
            
            _timerPanel.SetBackgroundColor(ColorSchemeConfig.BackgroundColor);
            _timerPanel.SetBorderColor(ColorSchemeConfig.BorderColor);
            _timerLabel.SetTextColor(ColorSchemeConfig.TextColor);
        }
    
        protected override void CreatePanel()
        {
            base.CreatePanel();
            Panel.AddToClassList(Constants.UI_TOP_PANEL_STYLE);

            _backIcon = CreateIMGUIContainer(IconsConfig.NextIcon);
            _backIcon.Rotate(180);
            _backButton = CreateButton(_backIcon, () => OnBackButtonClick?.Invoke());

            _toolIcon = CreateIMGUIContainer(IconsConfig.ShovelIcon);
            _toolType = PlayfieldToolType.Shovel;
            int numberOfTools = Enum.GetNames(typeof(PlayfieldToolType)).Length;
            _toolButton = CreateButton(_toolIcon, () => 
                {
                    SetToolButtonType((PlayfieldToolType)(((int)_toolType + 1) % numberOfTools));
                    OnToolButtonClick?.Invoke(_toolType);
                });
            ShowBackButton();

            _timerPanel = CreateTimer();

            _menuIcon = CreateIMGUIContainer(IconsConfig.MenuIcon);
            _menuButton = CreateButton(_menuIcon, () => OnMenuButtonClick?.Invoke());
        }
        
        private VisualElement CreateTimer()
        {
            var timerPanel = new VisualElement();
            Panel.Add(timerPanel);
            timerPanel.AddToClassList(Constants.UI_PANEL_STYLE);
            timerPanel.AddToClassList(Constants.UI_TOP_ELEMENT_STYLE);

            _timerLabel = new Label();
            _timerLabel.AddToClassList(Constants.UI_SMALL_TEXT_STYLE);
            timerPanel.Add(_timerLabel);
            SetTimerTextFromSeconds(0);

            return timerPanel;
        }

        protected override Button CreateButton(
            IMGUIContainer imguiContainer,
            Action buttonClicked,
            params string[] classesNames)
        {
            var button = base.CreateButton(
                imguiContainer, 
                buttonClicked, 
                Constants.UI_TOP_ELEMENT_STYLE);
            imguiContainer.AddToClassList(Constants.UI_SMALL_ICON_STYLE);
            return button;
        }
        #endregion
    }
}
